package mark.tom.sail;

import android.content.Context;

import androidx.annotation.NonNull;

public final class Sail {

    private final static String DEFAULT_ANALYTICS_TAG = "Sail";

    private static Facade facade = new Facade.EmptyFacade();
    protected static LogInterceptor mLogInterceptor;
    protected static FileStorage mFileStorage;

    public static SailBuilder newBuilder(@NonNull Context context) {
        facade = null;
        return new SailBuilder(context);
    }

    static void build(@NonNull SailBuilder builder) {
        mLogInterceptor = builder.logInterceptor();
        mFileStorage = builder.fileStorage();
        facade = new DefaultFacade(builder);
    }

    public static void collect(@NonNull AnalyticsRecord record) {
        collect(DEFAULT_ANALYTICS_TAG, record);
    }

    public static void collect(@NonNull String tag, @NonNull AnalyticsRecord record) {
        try {
            facade.collect(tag, record);
        } catch (Exception ex) {
            Util.log_e(mLogInterceptor, ex.getMessage());
        }
    }

    public static void report() {
        report(DEFAULT_ANALYTICS_TAG);
    }

    public static void report(@NonNull String... tags) {
        try {
            facade.report(tags);
        } catch (Exception ex) {
            Util.log_e(mLogInterceptor, ex.getMessage());
        }
    }
}
